
@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    @*<link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">*@
    <link href="~/layuimini/lib/layui-v2.6.3/css/layui.css" rel="stylesheet" />
    @*<link rel="stylesheet" href="../../css/public.css" media="all">*@
    <link href="~/layuimini/css/public.css" rel="stylesheet" />
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
    <div class="layui-form layuimini-form">

        <div class="layui-form-item">
            <label class="layui-form-label required">商品名字</label>
            <div class="layui-input-block">
                <input type="text" name="CommodityName" lay-verify="required" lay-reqtext="商品名字不能为空" placeholder="请输入商品名字" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">商品类型</label>
            <div class="layui-input-block">
                <select name="CategoryId" id="CategoryIdType">
                    <option value="">选择商品类型</option>

                </select>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label required">商品价格</label>
            <div class="layui-input-block">
                <input type="text" name="Price" lay-verify="required" lay-reqtext="商品价格不能为空" placeholder="请输入商品价格" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label ">商品详情</label>
            <div class="layui-input-block">
                <input type="text" name="Detail" placeholder="请输入商品详情" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品图片</label>
            <div class="layui-upload">
                <input type="hidden" name="CommodityImg" value="" class="layui-input" hidden="hidden" id="commodityImg">
                <div class="layui-upload-list">
                    <img class="layui-upload-img" src="" id="demo1" style="width:92px;min-height:80px;border: 1px solid #ddd;">
                    <p id="demoText"></p>
                </div>
                <button type="button" class="layui-btn" style="margin-left: 150px;" id="test1">上传图片</button>

            </div>
        </div>
        <br />
        <br />


        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
    @*<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>*@
    <script src="~/layuimini/lib/layui-v2.6.3/layui.js"></script>

    <script>
        //页面加载完才执行里面的js代码
        layui.use(['form'], function () {
            var form = layui.form,
                layer = layui.layer,
                upload = layui.upload,
                $ = layui.$;



            //异步获取商品下拉选数据集合
            $.ajax({
                url: 'http://localhost:31861/api/CommodityInfo/CommodityTypeOption',
                type: 'get',
                success: function (res) {

                    if (res.isSuccess) {
                        if (res.datas) {
                            //赋值商品下拉选
                            for (var i = 0; i < res.datas.length; i++) {
                                $('#CategoryIdType').append('<option value="' + res.datas[i].id + '">' + res.datas[i].categoryName + '</option>');
                            }

                            form.render('select'); //刷新select选择框渲染
                        }
                    }
                }
            });


            //常规使用 - 普通图片上传
            var uploadInst = upload.render({
                elem: '#test1'
                , url: 'http://localhost:31861/api/CommodityInfo/Upload' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
                , field: 'file'
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#demo1').attr('src', result); //图片链接（base64）
                        $('#commodityImg').val(result);
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    //上传成功的一些操作
                    //……
                    $('#demoText').html(''); //置空上传失败的状态
                }
                , error: function () {

                }
            });








            //监听提交
            form.on('submit(saveBtn)', function (data) {
                $.ajax({
                    url: 'http://localhost:31861/api/CommodityInfo/AddCommodityInfo',
                    data: data.field,
                    type: 'post',
                    success: function (res) {
                        if (res.isSuccess) {
                            var index = layer.alert("添加成功", {
                                title: '提示'
                            }, function () {

                                // 关闭弹出层
                                layer.close(index);
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                                //刷新表格数据
                                parent.reLoadTable();
                            });
                        } else {
                            layer.msg(res.msg);
                        }
                    }
                });

                return false;
            });

        });

    </script>
</body>
</html>
